package com.wzw.convert.tools;

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Date;

/**
 * word文件转PDF
 *
 * @auth 吴泽稳 2023-03-09
 */
public class Word2PDF implements OfficeTransUtil {
    private static Logger log = LoggerFactory.getLogger(Word2PDF.class);

    public void execute(String sourcePath) {

        String targetPath = sourcePath.substring(0, sourcePath.lastIndexOf("."));
//      sourcePath =  C:\Users\WuZewen\Desktop\文档转换格式测试\test.docx
        documents4jWordToPdf(sourcePath, targetPath + new Date().getTime() + ".pdf");

    }

    /**
     * 通过documents4j 实现word转pdf
     *
     * @param sourcePath 源文件地址 如 /root/example.doc
     * @param targetPath 目标文件地址 如 /root/example.pdf
     */
    public static void documents4jWordToPdf(String sourcePath, String targetPath) {
        File inputWord = new File(sourcePath);
        File outputFile = new File(targetPath);
        try {
            InputStream docxInputStream = new FileInputStream(inputWord);
            OutputStream outputStream = new FileOutputStream(outputFile);
            IConverter converter = LocalConverter.builder().build();
            boolean execute = converter.convert(docxInputStream)
                    .as(DocumentType.DOCX)
                    .to(outputStream)
                    .as(DocumentType.PDF).schedule().get();
//                    .execute();
            outputStream.close();
            docxInputStream.close();

            log.info("转换完毕 targetPath = {}", outputFile.getAbsolutePath());
            System.out.println("转换完毕 targetPath = " + outputFile.getAbsolutePath());
            converter.shutDown();
            return;
        } catch (Exception e) {
            log.error("[documents4J] word转pdf失败:{}", e.toString());
        }
    }

    public static void poiWordToPDF() throws Exception {
        FileInputStream fileInputStream = new FileInputStream("C:\\Users\\WuZewen\\Desktop\\文档转换格式测试\\2022年第45周（11月7日─11月11日）科技服务部_吴泽稳_工作计划及考核.docx");
        XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);
        PdfOptions pdfOptions = PdfOptions.create();
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\office\\test.pdf");
        PdfConverter.getInstance().convert(xwpfDocument, fileOutputStream, pdfOptions);
        fileInputStream.close();
        fileOutputStream.close();
    }

    /**
     * 通过docx4j 实现word转pdf
     *
     * @param sourcePath 源文件地址 如 /root/example.doc
     * @param targetPath 目标文件地址 如 /root/example.pdf
     */
//    public static void docx4jWordToPdf(String sourcePath, String targetPath) {
//        try {
//            WordprocessingMLPackage pkg = Docx4J.load(new File(sourcePath));
//            Mapper fontMapper = new IdentityPlusMapper();
//            fontMapper.put("隶书", PhysicalFonts.get("LiSu"));
//            fontMapper.put("宋体", PhysicalFonts.get("SimSun"));
//            fontMapper.put("微软雅黑", PhysicalFonts.get("Microsoft Yahei"));
//            fontMapper.put("黑体", PhysicalFonts.get("SimHei"));
//            fontMapper.put("楷体", PhysicalFonts.get("KaiTi"));
//            fontMapper.put("新宋体", PhysicalFonts.get("NSimSun"));
//            fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));
//            fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong"));
//            fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));
//            fontMapper.put("幼圆", PhysicalFonts.get("YouYuan"));
//            fontMapper.put("华文宋体", PhysicalFonts.get("STSong"));
//            fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong"));
//            fontMapper.put("等线", PhysicalFonts.get("SimSun"));
//            fontMapper.put("等线 Light", PhysicalFonts.get("SimSun"));
//            fontMapper.put("华文琥珀", PhysicalFonts.get("STHupo"));
//            fontMapper.put("华文隶书", PhysicalFonts.get("STLiti"));
//            fontMapper.put("华文新魏", PhysicalFonts.get("STXinwei"));
//            fontMapper.put("华文彩云", PhysicalFonts.get("STCaiyun"));
//            fontMapper.put("方正姚体", PhysicalFonts.get("FZYaoti"));
//            fontMapper.put("方正舒体", PhysicalFonts.get("FZShuTi"));
//            fontMapper.put("华文细黑", PhysicalFonts.get("STXihei"));
//            fontMapper.put("宋体扩展", PhysicalFonts.get("simsun-extB"));
//            fontMapper.put("仿宋_GB2312", PhysicalFonts.get("FangSong_GB2312"));
//            fontMapper.put("新細明體", PhysicalFonts.get("SimSun"));
//            pkg.setFontMapper(fontMapper);
//
//            Docx4J.toPDF(pkg, new FileOutputStream(targetPath));
//        } catch (Exception e) {
//            log.error("[docx4j] word转pdf失败:{}", e.toString());
//        }
//    }


}
